/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.core.launchers;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.debug.ui.launchConfigurations.JavaMainTab;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipsejcde.core.JCDECore;

/**
 * @author Amgad S Youssef
 *
 */
public class JCWDELauncher extends AbstractLauncher {
	private static JCWDELauncher jcwdeLauncher;

	private IJavaProject[] classpathProjects;

	private JCWDELauncher() {
	}

	public static ILauncher getLauncher() {
		if (jcwdeLauncher == null) {
			jcwdeLauncher = new JCWDELauncher();
		}
		return jcwdeLauncher;
	}

	public static ILauncher getLauncher(IJavaProject[] classpathProjects) {
		if (jcwdeLauncher == null) {
			jcwdeLauncher = new JCWDELauncher();
		}
		jcwdeLauncher.setClasspathProjects(classpathProjects);
		return jcwdeLauncher;
	}

	@Override
	protected Map<String, Serializable> getLaunchConfigurationAttributes(String arguments) throws Exception {
		Map<String, Serializable> attributes = new HashMap<String, Serializable>();
		attributes.put("org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", new Boolean(true));
		attributes.put("org.eclipse.debug.core.capture_output", new Boolean(true));
		attributes.put("org.eclipse.debug.ui.ATTR_CONSOLE_OUTPUT_ON", new Boolean(true));

		attributes.put(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, new Boolean(false));
		attributes.put(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "com.sun.javacard.jcwde.Main");
		attributes.put(JavaMainTab.ATTR_INCLUDE_EXTERNAL_JARS, new Boolean(true));
		ArrayList<String> classPathList = new ArrayList<String>();

		File jcwdeJar = new File(JCDECore.getCurrentJCDKHome() + File.separator + "lib" + File.separator + "jcwde.jar");
		File apduioJar = new File(JCDECore.getCurrentJCDKHome() + File.separator + "lib" + File.separator + "apduio.jar");
		File installerJar = new File(JCDECore.getCurrentJCDKHome() + File.separator + "lib" + File.separator + "installer.jar");
		File apiJar = new File(JCDECore.getCurrentJCDKHome() + File.separator + "lib" + File.separator + "api.jar");

		File[] jcjars = new File[] { jcwdeJar, apduioJar, installerJar, apiJar };
		for (int i = 0; i < jcjars.length; i++) {
			if (jcjars[i].getName().endsWith(".jar")) {
				IPath path = new Path(jcjars[i].getAbsolutePath());
				IRuntimeClasspathEntry runtimeClasspathEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(path);
				classPathList.add(runtimeClasspathEntry.getMemento());
			}
		}

		if (this.classpathProjects != null) {
			for (int i = 0; i < this.classpathProjects.length; i++) {
				IRuntimeClasspathEntry runtimeClasspathEntry = JavaRuntime.newProjectRuntimeClasspathEntry(this.classpathProjects[i]);
				classPathList.add(runtimeClasspathEntry.getMemento());
			}
		}

		attributes.put(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classPathList);
		attributes.put(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, arguments);
		return attributes;
	}

	@Override
	protected String getLaunchConfigurationTypeID() {
		return IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION;
	}

	@Override
	protected String getLauncherName() {
		return "JCWDE Simulator";
	}

	public static IStatus terminateJCWDE() {
		return jcwdeLauncher.terminate();
	}

	public IJavaProject[] getClasspathProjects() {
		return classpathProjects;
	}

	public void setClasspathProjects(IJavaProject[] classpathProjects) {
		this.classpathProjects = classpathProjects;
	}
}
